//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj忘记密码（邮箱）

#import "EmailForgotPasswordVC.h"
#import "LanguageController.h"
#import "CountryCodeVC.h"
#import "RegisterViewController.h"
#import "AllCountryCodeVC.h"
#import "NBPhoneNumberUtil.h"
#import "CaptchaView.h"
#import "EnterCodeController.h"
#import "MJConfirmLoadingView.h"
#import "RegisterEmailVC.h"
#import "ForgotPasswordVC.h"

@interface EmailForgotPasswordVC ()
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet MJUITextField *titleTextField;
@property (weak, nonatomic) IBOutlet MJConfirmLoadingView *confirmButton;
@property (weak, nonatomic) IBOutlet UIView *phoneNumberBgView;

@end

@implementation EmailForgotPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    
    [self.confirmButton.continueButton setTitle:MJUILocalizedString(@"Get Code") forState:0];
    self.tipLabel.hidden = true;
    [self.titleTextField mj_setClearButtonWithImage:[UIImage imageNamed:@"icon_del"]];
   
    if(self.emailString.length){
        self.titleTextField.text = self.emailString;
    }else{
        [self.titleTextField mj_becomeFirstResponder];
    }
    MJWeakSelf
    RAC(self.confirmButton, enabled) = [RACSignal combineLatest:@[self.titleTextField.rac_textSignal] reduce:^id _Nullable(NSString * text){
        
        if(text.length > 0 && [text mj_validateEmail]){
            weakSelf.tipLabel.hidden = true;

            return @(true);
        }
        return @(false);
    }];

    self.confirmButton.handleContinueBlock = ^{
        [weakSelf handleConfirmButtonClick];
    };
}

#pragma mark - 方法
- (void)requestData{
    
}

- (void)handleConfirmButtonClick{
    
    [self.titleTextField resignFirstResponder];
    
    //判断手机号码是否注册
    NSDictionary *params =  @{
        @"email": self.titleTextField.text,
    };
    MJWeakSelf
    [MJHttpNetWorking requestWithParams:params requestUri:user_findByEmail requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *model) {
        
        [weakSelf.confirmButton.loadingView stopAnimation];
        if(model.code == 200){
            if(model.data){
                [weakSelf sendAnjCcaptchaCode];
            }else{
                [weakSelf showNotRegisteredView];
            }
        }

    } failure:^(NSError *error) {
        [weakSelf.confirmButton.loadingView stopAnimation];

    }];

}

/// 滑块验证码
- (void)sendAnjCcaptchaCode {
    
    MJWeakSelf
    [CaptchaView showWithType: puzzle CompleteBlock:^(NSString *  result) {
        
        [weakSelf.confirmButton.loadingView startAnimation];

        NSLog(@"result=======================%@",result);
        NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{
            @"email": self.titleTextField.text,
            @"code": result,
        }];
        [MJHttpNetWorking requestWithParams:parms requestUri:captcha_sendEmail requestType:RequestTypePost showLoading:false success:^(MJBaseModel *model) {
            [weakSelf.confirmButton.loadingView stopAnimation];

            if (model.code ==  200){
                EnterCodeController *vc = [[EnterCodeController alloc]init];
                vc.captchaCodeType = CaptchaCodeTypeEmailForgotPassword;
                vc.email = weakSelf.titleTextField.text;
                [weakSelf.navigationController pushViewController:vc animated:true];
            }else if(model.code ==  412){
                [RoyHud showToast:MJUILocalizedString(@"Too frequent, Retry later.")];
            }
            
        } failure:^(NSError *error) {
            [weakSelf.confirmButton.loadingView stopAnimation];

        }];
    }];
}

- (void)showNotRegisteredView {
    self.tipLabel.hidden = false;

    MJWeakSelf
    MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
    configure.confirmButtonColor = UIColor.mj_appColor;
    configure.cancelTitle = MJUILocalizedString(@"Cancel_qx");
    configure.confirmTitle = MJUILocalizedString(@"OK_qd");
    configure.messageAlignmentCenter = true;
    configure.disableDismissWindow = true;
    NSString *messageString = [NSString stringWithFormat:@"%@\n%@",MJUILocalizedString(@"The email is not registered."),MJUILocalizedString(@"Sign up for a new account now?")];
    
    [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Email Not Registered") message:messageString configure:configure confirmBlock:^{
       
        RegisterEmailVC *vc = [[RegisterEmailVC alloc]init];
        vc.emailString = weakSelf.titleTextField.text;
        [weakSelf.navigationController pushViewController:vc animated:true];
        
    } cancelBlock:^{
       
    }];

}
- (IBAction)handlePhoneForgotPasswordButtonClick:(UIButton *)sender {
    if([self.navigationController mj_containTargetVCWithClass:ForgotPasswordVC.class]){
        [self.navigationController mj_popViewControllerWithTargetClass:ForgotPasswordVC.class];
    }else{
        ForgotPasswordVC *vc = [[ForgotPasswordVC alloc]init];
        [self.navigationController pushViewController:vc animated:true];
    }
}
#pragma mark - 代理

#pragma mark - 懒加载

@end
